package com.elvin.platform.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.elvin.platform.beans.Device;
import com.elvin.platform.beans.Dictionary;
import com.elvin.platform.service.DictionaryService;
import com.elvin.platform.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dictionaryController")
@Log4j2
@Api("字典管理")
public class DictionaryController {

	@Resource
	private DictionaryService service;

	@ApiIgnore
	@ApiOperation(value = "获取所有字典管理数据")
	@PostMapping("/queryAll")
	public Result<Object> queryAll() {
		Result<Object> result = new Result<>();
		try {
			List<Dictionary> selectList = service.selectList(null);
			result.success(selectList);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
	@ApiOperation(value = "分页获取所有数据")
	@PostMapping("/selectPage")
	public Object selectForPage(
			@ApiParam("当前页码") @RequestParam(value = "pageNumber", required = false)Integer pageNumber,
			@ApiParam("当前页显示条数") @RequestParam(value = "pageSize", required = false)Integer pageSize,
			@ApiParam("字典类型") @RequestParam(value = "type", required = false)String type,
			@ApiParam("字典名称") @RequestParam(value = "name", required = false)String name
	) {
		Result<Object> result = new Result<>();
		try {
			EntityWrapper<Dictionary> wrapper = new EntityWrapper<>();
			if (type != null && type.length() != 0) {
        wrapper.eq("type", type);
      }
      if (name != null && name.length() != 0) {
        wrapper.like("name", name);
      }
      int count = service.selectCount(wrapper);
			if (count > 0) {
        List<Dictionary> selectList = new ArrayList<>();
        if (pageNumber != null && pageSize != null) {
          Page<Dictionary> page = new Page<>(pageNumber,pageSize);
          service.selectPage(page,wrapper);
          selectList = page.getRecords();
        } else {
          selectList = service.selectList(wrapper);
        }
        result.success(selectList,count);
      } else {
        result.success(null,count);
      }

		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}
	@ApiOperation(value = "新增")
	@PostMapping("/insert")
	public Result<Object> insert(@RequestBody Dictionary entity) {
		Result<Object> result = new Result<>();
		try {
			service.insert(entity);
			result.success(entity, "新增成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "修改字典数据")
	@PostMapping("/update")
	public Result<Object> update(@RequestBody Dictionary entity) {
		Result<Object> result = new Result<>();
		try {
			service.updateById(entity);
			result.success("修改成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "删除字典数据")
	@PostMapping("/delete")
	public Result<Object> delete(@ApiParam(name="id",value="字典id",required=true) @RequestParam("id")  Integer id) {
		Result<Object> result = new Result<>();
		try {
			service.deleteById(id);
			result.success("删除成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}

	@ApiOperation(value = "根据ID获取字典数据")
	@PostMapping("/getById")
	public Result<Object> getById(@ApiParam(name="id",value="字典id",required=true) @RequestParam("id") Integer id) {
		Result<Object> result = new Result<>();
		try {
			Dictionary entity = new Dictionary();
			entity.setId(id);
			entity = service.selectById(entity);
			result.success(entity);
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null, e.getMessage());
		}
		return result;
	}


	@ApiOperation(value = "新增字典")
	@PostMapping("/insertDictionary")
	public Result<Object> insertDictionary(@RequestBody Dictionary entity) {
		Result<Object> result = new Result<>();
		try {
			service.insertDictionary(entity);
			result.success(entity,"新增成功");
		} catch (Exception e) {
			log.debug(e.getMessage());
			result.fail(null,e.getMessage());
		}
		return result;
	}


  @ApiOperation(value = "根据param3查询数据")
  @PostMapping("/getDevicesByParam3")
  public  Object  getDevicesByParam3(
      @ApiParam(name="param3",value="param3",required=true) @RequestParam("param3") String param3
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      EntityWrapper<Dictionary> wrapper = new EntityWrapper<>();
      if (param3 != null && param3.length() != 0) {
        wrapper.eq("param3", param3);
      }
      List<Dictionary> selectList = new ArrayList<>();
      selectList = service.selectList(wrapper);
      map.put("data", selectList);
      map.put("status", 0);
      map.put("message", "查询成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }


  @ApiOperation(value = "根据param4查询数据")
  @PostMapping("/getDevicesByParam4")
  public  Object  getDevicesByParam4(
      @ApiParam(name="param4",value="param4",required=true) @RequestParam("param4") String param4
  ) {
    Map<String, Object> map = new HashMap<String, Object>();
    try {
      EntityWrapper<Dictionary> wrapper = new EntityWrapper<>();
      if (param4 != null && param4.length() != 0) {
        wrapper.eq("param4", param4);
      }
      List<Dictionary> selectList = new ArrayList<>();
      selectList = service.selectList(wrapper);
      map.put("data", selectList);
      map.put("status", 0);
      map.put("message", "查询成功");
    } catch (Exception e) {
      log.debug(e.getMessage());
    }
    return map;
  }
}

